﻿''=============================================================================
''OMANE - Outils de calcul actuariat emprunteur
''Copyright © 2010 PIERRE TISSOT CONSEIL / NATIXIS ASSURANCES
''=============================================================================
Imports PTIC_Util
Imports System.Data.OleDb
Imports System.Collections.ObjectModel

''' <summary>
''' Objet générique fortement typé et observable implémentant IParam
''' </summary>
''' <typeparam name="typeParametre">Type du parametre</typeparam>
''' <remarks></remarks>
<Serializable()> Public MustInherit Class GeneriqueTypedParametre(Of typeParametre)
    Inherits BaseParametre

#Region "Création d'une instance"
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="nom">Nom du parametre</param>
    ''' <param name="description">Description du parametre</param>
    ''' <param name="methodes_calcul">Méthode de calculs</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal nom As String, _
                   ByVal description As String, _
                   ByVal methodes_calcul As Boolean(), _
                   ByVal initialValue As typeParametre)
        MyBase.New(nom, description, methodes_calcul)
        'Initialisation de la table des valeurs
        Dim dtble_Valeurs As New DataTable
        dtble_Valeurs.Columns.Add(nom, GetType(typeParametre))
        Dim r As DataRow = dtble_Valeurs.NewRow()
        r(nom) = initialValue
        dtble_Valeurs.Rows.Add(r)
        _valeurs = dtble_Valeurs
    End Sub
#End Region

#Region "Type"
    ''' <summary>
    ''' Type
    ''' </summary>
    ''' <remarks></remarks>
    Public Overrides ReadOnly Property Type() As System.Type
        Get
            Return GetType(typeParametre)
        End Get
    End Property
#End Region

#Region "Valeur courante non typée"
    ''' <summary>
    ''' Valeur en cours du paramètre (une des lignes de la DataTable (utile pour les moteurs de calcul))
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Property ValeurEnCoursAsObject As Object
        Get
            Return MyBase.ValeurEnCoursAsObject
        End Get
        Set(ByVal value As Object)
            Try
                MyBase.ValeurEnCoursAsObject = DirectCast(value, typeParametre)
            Catch
                MsgBox("Erreur de conversion")
            End Try
        End Set
    End Property
#End Region
#Region "Valeur courante Typée"
    ''' <summary>
    ''' Valeur courante du paramètre (utilisée par les moteur de calcul)
    ''' </summary>
    ''' <remarks></remarks>
    Public Property ValeursEnCours As typeParametre
        Get
            If Type.Name = "Double" Then
                Dim result As Object = CType(ValeurEnCoursAsObject, Double) + SimulDouble
                Return result
            Else
                Return ValeurEnCoursAsObject
            End If
        End Get
        Set(ByVal value As typeParametre)
            ValeurEnCoursAsObject = value
        End Set
    End Property
#End Region

#Region "Valeur unique typée"
    ''' <summary>
    ''' Valeur unique Typée. Renvoie nothing si le paramètre est décliné
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ValeurUnique As typeParametre
        Get
            Return IIf(_valeurs.Columns.Count = 1 And _valeurs.Rows.Count = 1, _valeurs.Rows(0)(0), Nothing)
        End Get
        Set(ByVal value As typeParametre)
            If value Is Nothing Then
                MsgBox("Vous devez renseigner une valeur pour le paramètre")
            ElseIf Not (Nature = ParamNature.Unique And value.Equals(ValeurUnique)) Then
                Dim b As Boolean = (Nom = csteParam.nomtble_maintien_incinvFRA)
                Dim dtble_Valeurs As New DataTable
                dtble_Valeurs.Columns.Add(Nom, GetType(typeParametre))
                Dim r As DataRow = dtble_Valeurs.NewRow()
                r(Nom) = value
                dtble_Valeurs.Rows.Add(r)
                _valeurs = dtble_Valeurs

                If Not ListeSegments Is Nothing Then
                    ListeSegments.Clear()
                End If

                RaiseEventHasChanged(Me, New EventArgs)
            End If
        End Set
    End Property
#End Region
#Region "Valeur unique non typée"
    ''' <summary>
    ''' Valeur unique non typée
    ''' </summary>
    ''' <remarks></remarks>
    Public Overrides ReadOnly Property ValeurUniqueAsObject() As Object
        Get
            Return ValeurUnique
        End Get
    End Property
#End Region

#Region "Gestion de l'état du paramètre (computed / not computed)"
    Protected _ParamEtat As ParamEtat
    Public MustOverride Property Etat As ParamEtat

    <NonSerialized()> Protected _GeneriqueTypedParametre_Etat As GeneriqueTypedParametre_EtatBase(Of typeParametre)
    Protected Property GeneriqueTypedParametre_Etat As GeneriqueTypedParametre_EtatBase(Of typeParametre)
        Get
            Return _GeneriqueTypedParametre_Etat
        End Get
        Set(ByVal value As GeneriqueTypedParametre_EtatBase(Of typeParametre))
            _GeneriqueTypedParametre_Etat = value
        End Set
    End Property
    Public Overrides Sub MAJValeurs()
        _GeneriqueTypedParametre_Etat.MAJValeurs()
    End Sub
    Protected Overrides Sub onListeSegmentsChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
        MyBase.onListeSegmentsChanged(sender, e)
        _GeneriqueTypedParametre_Etat.onListeSegmentsChanged(sender, e)
    End Sub
#End Region

End Class
